package org.ttitfly.workflow.engine.run;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

/**
 * 工作流实体基类
 * 
 * <pre>
 * 任何工作流实体都继承此类
 * </pre>
 * 
 * @author 潘海春
 * @version 2008-12-9
 */
@MappedSuperclass
public abstract class WFEntity implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 
	 * 唯一ID 
	 */
	@Id
	@Column(name = "ID")
	@GeneratedValue
	protected int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	/**
	 * 认为完全同类型并且ID相等的实体对象就相等
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (this == obj) {
			// 相同引用
			return true;
		}
		if ((obj != null) && (id > 0) && 
				(getClass().isInstance(obj) || obj.getClass().isInstance(this))) {
			// 完全同类型并且ID相等的entity对象就认为相等
			WFEntity target = (WFEntity) obj;
			return id == target.getId();
		}
		return super.equals(obj);
	}

	/**
	 * 覆写equals之后覆写hashCode方法
	 */
	@Override
	public int hashCode() {
		if (id > 0) {
			return id * 19;
		}
		return super.hashCode();
	}

}
